package com.greendragon.interceptor; 

import java.util.Map;

import org.apache.commons.lang.xwork.StringUtils;

import com.greendragon.control.constant.GlobalConstants;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

/**
 * 
 * 用户登入状态校验
 * 
 * @author wulibin, exceptsky@gmail.com, 2011-9-11 下午12:14:42
 * 
 */
public class LoginCheckInterceptor extends AbstractInterceptor{

    private static final long     serialVersionUID = 1L;

    @Override
    public String intercept(ActionInvocation invocation) throws Exception {
        ActionContext actionContext = invocation.getInvocationContext();
        //HttpServletRequest request = (HttpServletRequest) actionContext.get(StrutsStatics.HTTP_REQUEST);
        Map<String, Object> session = actionContext.getSession();
        if (session != null && session.get(GlobalConstants.SESSION_LOGIN_USER) != null) {
            return invocation.invoke();
        }
        setGoingToURL(session, invocation);
        return "login";
    }

    private void setGoingToURL(Map<String, Object> session, ActionInvocation invocation) {
        String url = "";
        String namespace = invocation.getProxy().getNamespace();
        if (StringUtils.isNotBlank(namespace) && !namespace.equals("/")) {
            url = url + namespace;
        }
        String actionName = invocation.getProxy().getActionName();
        if (StringUtils.isNotBlank(actionName)) {
            url = url + "/" + actionName + ".action";
        }
        session.put(GlobalConstants.SESSION_REDIRECT, url);
    }
}
 